package misc

import (
	"errors"
	"strings"
	"dhfshop/models"
	"github.com/astaxie/beego/orm"
)

type ShopConfig struct {
	ShopConfigId int64   `orm:"column(shop_config_id);pk;auto" json:"shop_config_id" form:"shop_config_id"` 
	CompanyId    int64   `orm:"column(company_id);index" json:"company_id" form:"company_id"` 
	UserId       int64   `orm:"column(user_id);index" json:"user_id" form:"user_id"`
	Name         string  `orm:"column(name);size(64);null" json:"name" form:"name"`
	Types        string  `orm:"column(types);size(64);null" json:"types" form:"types"` 
	Subtype      string  `orm:"column(subtype);size(64);null" json:"subtype" form:"subtype"` 
	Field        string  `orm:"column(field);size(128)" json:"field" form:"field"` 
	Value        string  `orm:"column(value);type(text)" json:"value" form:"value"`  
}

func init() {
    orm.RegisterModel(new(ShopConfig))   
}

func (u *ShopConfig) TableUnique() [][]string {
	return [][]string{
		[]string{"CompanyId", "UserId", "Field"},
	}
} 

func AddShopConfig(m *ShopConfig) (id int64, err error) {
	o := models.GetDefaultDB()
	id, err = o.Insert(m)
	return
}

func AddMultiShopConfigs(list []*ShopConfig) (err error) {
    o := models.GetDefaultDB()
    size := len(list)
	if size > 1 {
	    _, err = o.InsertMulti(size, list) 
	}else{
	   _, err = o.Insert(list[0])
	}
	return
}

func GetShopConfigById(id int64) (v *ShopConfig, err error) {
	o := models.GetDefaultDB()
	v = &ShopConfig{ShopConfigId: id}
	if err = o.Read(v); err == nil {
		return v, nil
	}
	return nil, err
}

func GetShopConfigByPartnerId(partnerId int64,feild string) (value string, err error) {
	o := models.GetDefaultDB()
	v := &ShopConfig{CompanyId:partnerId,Field:feild}
	if err = o.Read(v, "CompanyId", "Field"); err != nil {
		return
	}
	value = v.Value
	return
}

func GetAllShopConfigs(query map[string]string, fields []string, order_by map[string]string, 
    offset int, limit int) ([]*ShopConfig, int64,error) {
	o := models.GetDefaultDB()
	qs := o.QueryTable(new(ShopConfig))
	for k, v := range query {
		k = strings.Replace(k, ".", "__", -1)
		qs = qs.Filter(k, v)
	}
	cnt, err := qs.Count()
	// order by:
	var sortFields []string
	for k, v := range order_by {
		orderby := ""
		if v == "desc" {
			orderby = "-" + k
		} else if v == "asc" {
			orderby = k
		} else {
			return nil, 0, errors.New("Error: Invalid order. Must be either [asc|desc]")
		}
		sortFields = append(sortFields, orderby)
	}
	qs = qs.OrderBy(sortFields...)
	var lists []*ShopConfig
	if _, err := qs.Limit(limit, offset).All(&lists, fields...); err == nil {
		return lists, cnt, nil
	}
	return nil, 0, err
}

func UpdateShopConfigById(m *ShopConfig, k ...string) (err error) {
	o := models.GetDefaultDB()
	v := ShopConfig{ShopConfigId: m.ShopConfigId}
	if err = o.Read(&v); err != nil {
		return err
	}else{
	    if _, err = o.Update(m, k...); err != nil {
		   return err
		}
	}
	return nil
}

func DeleteShopConfig(id int64) (err error) {
	o := models.GetDefaultDB()
	v := ShopConfig{ShopConfigId: id}
	if err = o.Read(&v); err == nil {
		if _, err = o.Delete(&v); err == nil {
			return nil
		}
	}
	return
}

